<?php include_once($_SERVER['DOCUMENT_ROOT']."/controller/common/common_setting_unit.php");// 引入通用的设置文件 ?>
<?php include_once($_SERVER['DOCUMENT_ROOT']."/controller/common/module/head.php"); // 引入通用的头部 ?>
<?php include_once("../controller/data_add_batch.php");// 当前页面的控制器 ?>
<body>
<!--顶部栏-->
<div id="topbar"> <?php include $_SERVER['DOCUMENT_ROOT'].'/controller/common/module/topbar.php'; ?></div>
<!--容器-->
<div id="container" class="<? echo $now_extender ?>">
    <!--侧边栏-->
    <div id="sidebar"><?php include $_SERVER['DOCUMENT_ROOT'].'/controller/common/module/sidebar.php'; ?></div>
    <!--主内容-->
    <div id="content">
        <!--标题设定-->
        <?php $page_title='批量插入';?>
        <?php include $_SERVER['DOCUMENT_ROOT'].'/controller/common/module/mod-page-title.php'; ?>
        <?//============================================?>
        <?//以下开发需要注重的部分，主要写前端?>
        <?//============================================?>


        <?php include 'module/mod-mulity-add-function.php'; ?>












           <div class="detail-box">
               <?php $section_title='推文列表';?>
               <?php include $_SERVER['DOCUMENT_ROOT'].'/controller/common/module/mod-section-title.php'; ?>
               <ul class="detail-boxing">
                   <form id="data_form" method="post" class="layui-form"
                         action ="data_add_process.php?<? echo $data['url_parameter'] ?>"
                         enctype="multipart/form-data">


                       <table class="layui-table">
                           <colgroup>
                               <col width="65px">
                               <col width="650px">
                               <col width=" ">
                               <col width="220px">
                               <col width=" ">
                               <col width="65px">
                           </colgroup>
                           <thead>
                           <tr>
                               <th>编号</th>
                               <th>内容</th>
                               <th>标签</th>
                               <th>日期</th>
                               <th>媒体</th>
                               <th>操作</th>
                           </tr>
                           </thead>
                           <tbody>
                           <? for($i=0;$i<count($_result);$i++){?>
                           <tr id="tr<?echo $i?>">
                               <td><?echo $i+1?></td>
                               <td>

                                   <textarea name="ai_content[]" id=content_<?echo $i?>
                                             placeholder="请输入内容"
                                          class="layui-textarea" style="width: 600px;height:100px"  ><?echo $_result[$i]['content']?></textarea>
                               </td>
                               <td>
                                   <input type="text" name="ai_tag[]" id=tag_<?echo $i?>
                                          placeholder="根据库中，随机生成三个标签"
                                          autocomplete="off" class="layui-input" value="<?echo $_result[$i]['tag']?>">
                               </td>
                               <td>
                                   <input type="text" name="publish_date[]" id=date_<?echo $i?>
                                          placeholder="发布日期"
                                          autocomplete="off" class="layui-input" value="<?echo $_result[$i]['date']?>">
                               </td>
                                <td>
<!--                                    创建一个select出来-->
                                    <select name="ai_media[]" id=media_<?echo $i?>
                                            lay-verify="required" lay-search="">
                                        <?foreach ($picture_folder as $key=>$value){?>
                                        <option value="<?echo $value?>"
                                                file_number="<?echo $_image_number[$value]?>" ><?echo $value?>(<?echo $_image_number[$value]?>)</option>
                                        <?;}?>
                                    </select>
                                    <span class="notice">只传编号,读取编号中的6张图上传<br/>
                                        文件夹用8位数字,文件用4位数字6开头</span>
                                </td>

<!--                               <input type="hidden" name="file_number[]" id="file_number_--><?//echo $i?><!--" value="--><?//echo $_image_number[$value]?><!--" />-->

                               <td>
                                   <span class="layui-btn" id="send_btn_<?echo $i?>" onclick="send_to_fb_api(<?echo $i?>)">发布</span>
                               </td>
                           </tr>
                           <?;}?>

                           </tbody>
                       </table>



                   </form>
               </ul>
           </div>







        <?//============================================?>
        <?//以下JS部分需要根据实际情况去加载?>
        <?//============================================?>

    </div>
    <!--content end-->
<br class="clearfloat"/>
<!--container  end-->
</div>
<!--ajax生成表格-->
<!--<script src="../asset/js/set_sheet.js"></script>-->
<script src="/controller/common/asset/js/controller.js"></script>
<script>

    //传递参数到fb的api
    function send_to_fb_api(id) {
        console.log("执行数据id:",id);

        //弹出遮罩图层，用于等待
        show_message_layer();

        //让按钮失效
        $("#send_btn_" + id).attr("disabled", true);
        //获取点击按钮当前所属的tr内的所有input select的内容，用于传递到api
        var page_id = $("#page_id").val();
        var accesstoken = $("#accesstoken").val();
        var content = $("#content_" + id).val();
        var tag = $("#tag_" + id).val();
        var date = $("#date_" + id).val();
        var media = $("#media_" + id).val();

        //$("#media_" + id)下面选中的option的file_number属性的值
        var file_number = $("#media_" + id + " option:selected").attr("file_number");



        content=content+tag;

        console.log("page_id:", page_id);
        console.log("accesstoken:", accesstoken);
        console.log("content:", content.substring(0, 15) + (content.length > 15 ? "..." : "")); // 限制 content 输出为 15 个字符
        console.log("tag:", tag);
        console.log("date:", date);
        console.log("media:", media);
        console.log("文件数:", file_number);


        //如果content为空，则终止程序云运行
        if (content == "") {
            alert("请输入内容");
            hide_message_layer();

            $("#send_btn_" + id).attr("disabled", false);
            return;
        }





        $.ajax({
            type: "POST",
            url: "http://dm.crossbow.enpingshi.com/api/facebook/post_with_images.php",
            data: {
                "page_id": page_id,
                "accesstoken": accesstoken,
                "content": content,
                "tag": tag,
                "date": date,
                "media": media,
                "file_number": file_number
            },
            success: function (data) {
                console.log(data);
                hide_message_layer();
                //如果包含字符串Post successfully，则删除本行
                if (data.indexOf("successfully") > -1) {
                    //$("#tr" + id).remove();

                    //id是send_btn_+id的按钮
                    //如果文字是发布，就修改成发布A，如果是发布A就修改成发布B
                    if ($("#send_btn_" + id).html() == "发布")
                    {
                        $("#send_btn_" + id).html("发布A");
                    } else {
                        $("#send_btn_" + id).html("发布");
                    }


                    var date_next    = $("#date_" + (id + 1)).val();
                    var content_next = $("#content_" + (id + 1)).val();

                    console.log("Checking next fields for id: " + (id + 1));
                    console.log("Selector for date: #date_" + (id + 1));
                    console.log("Selector for content: #content_" + (id + 1));
                    console.log("Date next value: " + date_next);
                    if (typeof content_next !== 'undefined' && content_next !== null && content_next !== '') {
                        console.log("Content next value: :", content_next.substring(0, 15) + (content_next.length > 15 ? "..." : ""));
                    } else {
                        console.error("content_next 为空或未定义");
                        alert("发布完毕");

                    }


                    //如果date_next为空，则等待5秒钟，再次执行send_to_fb_api(id)，id需要当前数+1



                    if (
                        (date_next != "undefined") &&
                        (date_next !== null) &&
                        (content_next !== null) &&
                        (content_next != "undefined")
                        )


                    {

                        console.log("休息两秒，继续执行");
                        setTimeout(function () {
                            send_to_fb_api(id + 1);
                        }, 2000);
                    }
                    else
                    {
                        alert("发布完毕");
                        console.log("找不到值了");
                        console.log("content_next",content_next);
                        console.log("date_next",date_next);
                    }

                } else {
                    alert(data);
                }




            }
            , error: function (data) {
                console.log(data);
                hide_message_layer();
                alert("出错了");
                $("#send_btn_" + id).attr("disabled", false);
            }
        });
        return false;
    }
</script>




<!--底部栏-->
<?php include $_SERVER['DOCUMENT_ROOT']."/controller/common/module/footer.php"; ?>

